package com.example.sample.list.data;

import com.ohos.volley.ext.LocalHiLog;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;

public class ListHolder implements ListContainer.ItemClickedListener {
    private static final String TAG = "ListHolder";

    private Ability mSlice;

    private DirectionalLayout mRootLayout;

    private ListContainer listContainer;

    private ListItemProvider listItemProvider;

    public ListHolder(Ability abilitySlice) {
        mSlice = abilitySlice;
        listItemProvider = new ListItemProvider(abilitySlice);
    }

    public ComponentContainer createComponent() {
        mRootLayout = new DirectionalLayout(mSlice);
        listContainer = new ListContainer(mSlice);
        final int matchParent = ComponentContainer.LayoutConfig.MATCH_PARENT;
        mRootLayout.addComponent(listContainer, new ComponentContainer.LayoutConfig(matchParent, matchParent));
        listContainer.setItemProvider(listItemProvider);

        listContainer.setItemClickedListener(this);

        return mRootLayout;
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int position, long id) {
        ListItemProvider.SampleConfig sampleConfig = (ListItemProvider.SampleConfig) listContainer.getItemProvider().getItem(position);
        Class clazz = sampleConfig.targetClass;

        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(clazz.getPackage().getName())
                .withAbilityName(clazz.getName())
                .build();
        intent.setOperation(operation);
        LocalHiLog.info("ListHolder", clazz.getPackage().getName() + "    " + clazz.getName());

        mSlice.startAbility(intent);
    }
}
